package com.san.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.san.base.dao.DictDao;
import com.san.base.domain.DictDO;
import com.san.base.domain.Tree;
import com.san.base.service.DictService;
import com.san.common.utils.BuildTree;
import com.san.common.utils.Constant;


@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictDao dictMapper;

    @Override
    public DictDO getById(Long id) {
        return dictMapper.getById(id);
    }

    @Override
    public List<DictDO> list(Map<String, Object> map) {
        return dictMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return dictMapper.count(map);
    }

    @Override
    public int save(Object obj) {
        return dictMapper.save(obj);
    }

    @Override
    public int doEdit(Object obj) {
        return dictMapper.doEdit(obj);
    }

    @Override
    public int remove(Long id) {
        return dictMapper.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return dictMapper.batchRemove(ids);
    }

    @Override

    public List<DictDO> listType() {
        return dictMapper.listType();
    }

    @Override
    public String getName(String code,String value) {
    	String rString = "";
    	
        Map<String, Object> param = new HashMap<String, Object>(16);
        param.put("value", code);
        List<DictDO> parent = dictMapper.list(param);
        if (null != parent && parent.size() > 0) {
        	param = new HashMap<String, Object>(16);
            param.put("parentId", parent.get(0).getId());
            param.put("value", value);
            
            List<DictDO> child = dictMapper.list(param);
            if (null != child && child.size() > 0) {
            	rString = child.get(0).getName();
            }
        }
         
        return rString;
    }

    @Override
    public List<DictDO> getSexList() {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "sex");
        return dictMapper.list(param);
    }

    @Override
    public List<DictDO> listByType(String type) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", type);
        return dictMapper.list(param);
    }

	@Override
	public Tree<DictDO> getTree() {
		List<Tree<DictDO>> trees = new ArrayList<Tree<DictDO>>();
		
		 Map<String, Object> param = new HashMap<>(16);
        param.put("type", Constant.DICT_TYPE_CLASS);
		List<DictDO> dicts = dictMapper.list(param);
		
		for (DictDO dict : dicts) {
			Tree<DictDO> tree = new Tree<DictDO>();
			tree.setId(dict.getId().toString());
			tree.setParentId(dict.getParentId().toString());
			tree.setText(dict.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			state.put("mType", "dept");
			tree.setState(state);
			trees.add(tree);
		}
		
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<DictDO> t = BuildTree.build(trees, true);
		return t;
	}

	@Override
	public List<DictDO> getBusiType() {
		return dictMapper.getBusiType();
	}

	@Override
	public List<DictDO> getBusiTypeNode(Long parentId) {
		return dictMapper.getBusiTypeNode(parentId);
	}

	@Override
	public List<DictDO> getByValue(String value) {
		return dictMapper.getByValue(value);
	}

}
